package com.iespring.service.poromotion;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.promotion.bo.MallCouponBo;
import com.iespring.bean.admin.promotion.bo.MallCouponForCreateBo;
import com.iespring.bean.admin.promotion.po.MallCoupon;
import com.iespring.bean.admin.promotion.po.MallCouponExample;
import com.iespring.bean.admin.promotion.po.MallCouponUser;
import com.iespring.bean.admin.promotion.po.MallCouponUserExample;
import com.iespring.bean.admin.promotion.vo.MallCouponVo;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.mapper.admin.MallCouponMapper;
import com.iespring.mapper.admin.MallCouponUserMapper;
import com.iespring.util.RandomStringUtil;
import com.iespring.util.StrUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author:ZhangShuPeng
 * @Date:2022/12/29/15:05
 * @Description:
 */
@Service
public class PromotionCouponServiceImpl implements PromotionCouponService {

    @Autowired
    MallCouponMapper mallCouponMapper;

    @Autowired
    MallCouponUserMapper mallCouponUserMapper;


    /**
     * page=1&limit=20&name=%E7%BE%8E%E5%9B%A2&type=2&status=0&sort=add_time&order=desc
     *
     * @param info   分页:page/limit  排序:sort/order
     * @param name   优惠券标题
     * @param type   优惠券类型
     * @param status 优惠券状态
     * @return
     */
    @Override
    public CommonData<MallCoupon> list(BasePageInfo info, String name, Short type, Short status) {

        PageHelper.startPage(info.getPage(), info.getLimit());

        MallCouponExample mallCouponExample = new MallCouponExample();
        mallCouponExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MallCouponExample.Criteria criteria = mallCouponExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        if (!StrUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StrUtil.isEmpty(StrUtil.valueOf(type))) {
            criteria.andTypeEqualTo(type);
        }
        if (!StrUtil.isEmpty(StrUtil.valueOf(status))) {
            criteria.andStatusEqualTo(status);
        }

        List<MallCoupon> mallCoupons = mallCouponMapper.selectByExample(mallCouponExample);
        PageInfo<MallCoupon> mallCouponPageInfo = new PageInfo<>(mallCoupons);
//        int pageSize = mallCouponPageInfo.getPageSize();
//        int pages = mallCouponPageInfo.getPages();
//        int pageNum = mallCouponPageInfo.getPageNum();
//        int total = (int) mallCouponPageInfo.getTotal();
//        List<MallCoupon> list = mallCouponPageInfo.getList();
//
//        CommonData<MallCoupon> data = new CommonData<MallCoupon>(total, pages, pageSize, pageNum, list);


        return CommonData.data(mallCouponPageInfo);
    }

    @Override
    public BaseRespVo create(MallCouponForCreateBo mallCouponForCreateBo) {

        BigDecimal discount = mallCouponForCreateBo.getDiscount();  //优惠金额
        BigDecimal min = mallCouponForCreateBo.getMin();            //最低消费
        Integer total = mallCouponForCreateBo.getTotal();           //优惠券数量
        Short limit = mallCouponForCreateBo.getLimit();             //用户领券限制数量
        String name = mallCouponForCreateBo.getName();              //优惠券名称
        String desc = mallCouponForCreateBo.getDesc();              //优惠券介绍
        String tag = mallCouponForCreateBo.getTag();                //优惠券标签
        Short type = mallCouponForCreateBo.getType();               //优惠券赠送类型
        Short status = mallCouponForCreateBo.getStatus();           //优惠券状态
        Short days = mallCouponForCreateBo.getDays();               //基于领取时间的有效天数
        Short goodsType = mallCouponForCreateBo.getGoodsType();     //商品限制类型
        String[] goodsValue = mallCouponForCreateBo.getGoodsValue();//商品限制值
        Short timeType = mallCouponForCreateBo.getTimeType();       //有限时间限制
        Date endTime = mallCouponForCreateBo.getEndTime();
        Date startTime = mallCouponForCreateBo.getStartTime();


        MallCoupon mallCoupon = new MallCoupon();


        if (discount.compareTo(min) > 0) {
            return BaseRespVo.error("参数异常!优惠金额大于最低消费金额!", 500);
        }
        Integer limit1 = (int) limit;
        if (limit1.compareTo(total) > 0) {
            return BaseRespVo.error("参数异常!用户领取优惠券数量不能大于优惠券总数量!", 500);
        }

        if (days <= 0) {
            return BaseRespVo.error("参数异常！优惠券有效期时长必须大于零！", 500);
        }
        String code = RandomStringUtil.getRandomString();

        mallCoupon.setDiscount(discount);
        mallCoupon.setMin(min);
        mallCoupon.setLimit(limit);
        mallCoupon.setTotal(total);
        mallCoupon.setDays(days);
        mallCoupon.setDesc(desc);
        mallCoupon.setGoodsType(goodsType);
        mallCoupon.setGoodsValue(goodsValue);
        mallCoupon.setName(name);
        mallCoupon.setStatus(status);
        mallCoupon.setTag(tag);
        mallCoupon.setTimeType(timeType);
        mallCoupon.setType(type);
        mallCoupon.setStartTime(startTime);
        mallCoupon.setEndTime(endTime);
        mallCoupon.setCode(code);

//        if (!StrUtil.isEmpty(name)) {
//            mallCoupon.setName(name);
//        }
//        if (!StrUtil.isEmpty(desc)) {
//            mallCoupon.setDesc(desc);
//        }
//        if (!StrUtil.isEmpty(tag)) {
//            mallCoupon.setTag(tag);
//        }

        mallCoupon.setAddTime(new Date());
        mallCoupon.setUpdateTime(new Date());
        mallCouponMapper.insertSelective(mallCoupon);

        MallCouponVo mallCouponVo = new MallCouponVo();

        mallCouponVo.setDiscount(mallCoupon.getDiscount());
        mallCouponVo.setMin(mallCoupon.getMin());
        mallCouponVo.setLimit(mallCoupon.getLimit());
        mallCouponVo.setTotal(mallCoupon.getTotal());
        mallCouponVo.setDays(mallCoupon.getDays());
        mallCouponVo.setDesc(mallCoupon.getDesc());
        mallCouponVo.setGoodsType(mallCoupon.getGoodsType());
        mallCouponVo.setGoodsValue(mallCoupon.getGoodsValue());
        mallCouponVo.setId(mallCoupon.getId());
        mallCouponVo.setName(mallCoupon.getName());
        mallCouponVo.setStatus(mallCoupon.getStatus());
        mallCouponVo.setTag(mallCoupon.getTag());
        mallCouponVo.setTimeType(mallCoupon.getTimeType());
        mallCouponVo.setType(mallCoupon.getType());
        mallCouponVo.setAddTime(mallCoupon.getAddTime());
        mallCouponVo.setUpdateTime(mallCoupon.getUpdateTime());


        BaseRespVo baseRespVo = BaseRespVo.ok(mallCouponVo);

        return baseRespVo;
    }

    @SneakyThrows
    @Override
    public BaseRespVo update(MallCouponBo mallCouponBo) {


//        if (!discount.toString().matches(".*[0-9]+.*") || !min.toString().matches(".*[0-9]+.*") ||
//                !total.toString().matches(".*[0-9]+.*") || !limit.toString().matches(".*[0-9]+.*") ||
//                !days.toString().matches(".*[0-9]+.*")) {
//            throw new InvalidParamException("请正确输入参数!");
//        }
        BigDecimal discount = mallCouponBo.getDiscount();  //优惠金额
        BigDecimal min = mallCouponBo.getMin();            //最低消费
        Integer total = mallCouponBo.getTotal();           //优惠券数量
        Short limit = mallCouponBo.getLimit();             //用户领券限制数量
        String name = mallCouponBo.getName();              //优惠券名称
        String desc = mallCouponBo.getDesc();              //优惠券介绍
        String tag = mallCouponBo.getTag();                //优惠券标签
        Short type = mallCouponBo.getType();               //优惠券赠送类型
        Short status = mallCouponBo.getStatus();           //优惠券状态
        Short days = mallCouponBo.getDays();               //基于领取时间的有效天数
        Short goodsType = mallCouponBo.getGoodsType();     //商品限制类型
        String[] goodsValue = mallCouponBo.getGoodsValue();//商品限制值
        Short timeType = mallCouponBo.getTimeType();       //有限时间限制
        Date addTime = mallCouponBo.getAddTime();
        Boolean deleted = mallCouponBo.getDeleted();
        Integer id = mallCouponBo.getId();
        Date updateTime = mallCouponBo.getUpdateTime();


        MallCoupon mallCoupon = new MallCoupon();


        if (discount.compareTo(min) > 0) {
            return BaseRespVo.error("参数异常!优惠金额大于最低消费金额!", 500);
        }
        Integer limit1 = (int) limit;
        if (limit1.compareTo(total) > 0) {
            return BaseRespVo.error("参数异常!用户领取优惠券数量不能大于优惠券总数量!", 500);
        }

        if (days <= 0) {
            return BaseRespVo.error("参数异常！优惠券有效期时长必须大于零！", 500);
        }

        mallCoupon.setDiscount(discount);
        mallCoupon.setMin(min);
        mallCoupon.setLimit(limit);
        mallCoupon.setTotal(total);
        mallCoupon.setDays(days);
        mallCoupon.setDeleted(deleted);
        mallCoupon.setDesc(desc);
        mallCoupon.setGoodsType(goodsType);
        mallCoupon.setGoodsValue(goodsValue);
        mallCoupon.setId(id);
        mallCoupon.setName(name);
        mallCoupon.setStatus(status);
        mallCoupon.setTag(tag);
        mallCoupon.setTimeType(timeType);
        mallCoupon.setType(type);

//        if (!StrUtil.isEmpty(name)) {
//            mallCoupon.setName(name);
//        }
//        if (!StrUtil.isEmpty(desc)) {
//            mallCoupon.setDesc(desc);
//        }
//        if (!StrUtil.isEmpty(tag)) {
//            mallCoupon.setTag(tag);
//        }

        mallCoupon.setAddTime(addTime);
        mallCoupon.setUpdateTime(updateTime);
        mallCouponMapper.insertSelective(mallCoupon);

        MallCouponVo mallCouponVo = new MallCouponVo();

        mallCouponVo.setDiscount(mallCoupon.getDiscount());
        mallCouponVo.setMin(mallCoupon.getMin());
        mallCouponVo.setLimit(mallCoupon.getLimit());
        mallCouponVo.setTotal(mallCoupon.getTotal());
        mallCouponVo.setDays(mallCoupon.getDays());
        mallCouponVo.setDeleted(mallCoupon.getDeleted());
        mallCouponVo.setDesc(mallCoupon.getDesc());
        mallCouponVo.setGoodsType(mallCoupon.getGoodsType());
        mallCouponVo.setGoodsValue(mallCoupon.getGoodsValue());
        mallCouponVo.setId(mallCoupon.getId());
        mallCouponVo.setName(mallCoupon.getName());
        mallCouponVo.setStatus(mallCoupon.getStatus());
        mallCouponVo.setTag(mallCoupon.getTag());
        mallCouponVo.setTimeType(mallCoupon.getTimeType());
        mallCouponVo.setType(mallCoupon.getType());
        mallCouponVo.setAddTime(mallCoupon.getAddTime());
        mallCouponVo.setUpdateTime(mallCoupon.getUpdateTime());


        BaseRespVo baseRespVo = BaseRespVo.ok(mallCouponVo);


        return baseRespVo;
    }

    @Override
    public int delete(MallCoupon mallCoupon) {

        mallCoupon.setDeleted(true);
        int rows = mallCouponMapper.updateByPrimaryKeySelective(mallCoupon);

        return rows;
    }

    /**
     * @param info     分页:page/limit  排序:sort/order
     * @param couponId 优惠券Id
     * @return
     */
    @SneakyThrows
    @Override
    public CommonData<MallCouponUser> listuser(BasePageInfo info, int couponId) {

        PageHelper.startPage(info.getPage(), info.getLimit());

        MallCouponUserExample mallCouponUserExample = new MallCouponUserExample();
        mallCouponUserExample.setOrderByClause(info.getSort() + "" + info.getOrder());
        MallCouponUserExample.Criteria criteria = mallCouponUserExample.createCriteria();

        criteria.andCouponIdEqualTo(couponId);

        List<MallCouponUser> mallCouponUsers = mallCouponUserMapper.selectByExample(mallCouponUserExample);
        PageInfo<MallCouponUser> mallCouponUserPageInfo = new PageInfo<>(mallCouponUsers);
//        int pageNum = mallCouponUserPageInfo.getPageNum();
//        int pages = mallCouponUserPageInfo.getPages();
//        int pageSize = mallCouponUserPageInfo.getPageSize();
//        int total = (int) mallCouponUserPageInfo.getTotal();
//        List<MallCouponUser> list = mallCouponUserPageInfo.getList();
//
//        CommonData data = new CommonData(total, pages, pageSize, pageNum, list);

        return CommonData.data(mallCouponUserPageInfo);
    }

    @Override
    public MallCoupon read(int id) {

        MallCoupon mallCoupon = mallCouponMapper.selectByPrimaryKey(id);

        return mallCoupon;
    }
}
